<template>
  <div class="app-container">
    <el-form :inline="true" v-show="showSearch" :model="queryParams" ref="queryRef" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入供应商名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 240px">
          <el-option :value="1" label="启用" />
          <el-option :value="0" label="停用" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['ug:supplier:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ug:supplier:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ug:supplier:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="ID" align="center" prop="supplierId" width="80" />
      <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="手机" align="center" prop="mobile" width="120" />
      <el-table-column label="状态" align="center" key="status">
        <template #default="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="(val)=>handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template #default="scope"><span>{{ parseTime(scope.row.createTime) }}</span></template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ug:supplier:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ug:supplier:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" maxlength="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机" prop="mobile">
              <el-input v-model="form.mobile" maxlength="20" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">启用</el-radio>
                <el-radio :label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开户行" prop="bankName">
              <el-input v-model="form.bankName" maxlength="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount">
              <el-input v-model="form.bankAccount" maxlength="64" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" type="textarea" maxlength="255" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Supplier">
import { listSupplier, addSupplier, updateSupplier, delSupplier, changeSupplierStatus, getSupplier } from '@/api/ug/supplier'

const { proxy } = getCurrentInstance()
const loading = ref(true)
const showSearch = ref(true)
const list = ref([])
const total = ref(0)
const open = ref(false)
const title = ref('')
const ids = ref([])
const single = ref(true)
const multiple = ref(true)

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, name: undefined, status: undefined },
  rules: {
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  },
})
const { form, queryParams, rules } = toRefs(data)

function getList() {
  loading.value = true
  listSupplier(queryParams.value).then(res => {
    loading.value = false
    list.value = res.data.list
    total.value = res.data.total
  })
}

function handleQuery() { queryParams.value.pageNum = 1; getList() }
function resetQuery() { proxy.resetForm('queryRef'); handleQuery() }

function handleSelectionChange(selection) {
  ids.value = selection.map((i)=>i.supplierId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function reset() {
  form.value = { supplierId: undefined, name: undefined, contact: undefined, mobile: undefined, address: undefined, bankName: undefined, bankAccount: undefined, status: 1 }
  proxy.resetForm('formRef')
}

function handleAdd() { reset(); open.value = true; title.value = '新增供应商' }

function handleUpdate(row) {
  reset()
  const id = row?.supplierId || ids.value[0]
  getSupplier(id).then(res=>{ form.value = res.data; open.value = true; title.value='编辑供应商' })
}

function handleDelete(row) {
  const delIds = row?.supplierId || ids.value
  proxy.$modal.confirm('是否确认删除选中数据？').then(()=>delSupplier(delIds)).then(()=>{ proxy.$modal.msgSuccess('删除成功'); getList() })
}

function handleStatusChange(row) {
  changeSupplierStatus(row.supplierId, row.status).then(()=>{ proxy.$modal.msgSuccess('操作成功') })
}

function submitForm() {
  proxy.$refs['formRef'].validate((valid)=>{
    if(!valid) return
    if(form.value.supplierId) {
      updateSupplier(form.value).then(()=>{ proxy.$modal.msgSuccess('修改成功'); open.value=false; getList() })
    } else {
      addSupplier(form.value).then(()=>{ proxy.$modal.msgSuccess('新增成功'); open.value=false; getList() })
    }
  })
}

function cancel() { open.value=false; reset() }

getList()
</script>
